package com.health.tools;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.health.NovaApplication;
import com.health.db.FDbManager;
import com.health.tablebean.ARCHIVE_ASSESSMENTGUIDE;
import com.health.tablebean.ARCHIVE_ASSISTCHECK;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.ARCHIVE_CUSTOMERBASEINFO;
import com.health.tablebean.ARCHIVE_GENERALCONDITION;
import com.health.tablebean.ARCHIVE_LIFESTYLE;
import com.health.tablebean.CD_DIABETESFOLLOWUP;
import com.health.tablebean.CD_HYPERTENSIONFOLLOWUP;
import com.health.tablebean.CHILD_NEWBORN_FOLLOWUP;
import com.health.tablebean.CHILD_ONE2THREE_YEAR_OLD;
import com.health.tablebean.CHILD_THREE2SIX_YEAR_OLD;
import com.health.tablebean.CHILD_WITHIN_ONE_YEAR_OLD;
import com.health.tablebean.JTYSCustomerDoctorCont;
import com.health.tablebean.JTYSDoctorEvaluateBean;
import com.health.tablebean.JTYS_SignPictureData;
import com.health.tablebean.OLDER_SELFCAREABILITY;
import com.health.tablebean.OLD_MEDICINE_CN;
import com.health.tablebean.webservice.ChildThree2SixYearOld;
import com.health.tablebean.webservice.ChildhealthNeonate;

import android.util.Log;

/**
 * 上传数据工具类
 */
public class JX_UploadData {
    public static final String TYPE_PERSONALINFO = "PersonalInfo";// 个人基本信息
    public static final String TYPE_HYPERTENSION = "Hypertension";// 高血压
    public static final String TYPE_DIABETES = "Diabetes";// 糖尿病
    public static final String TYPE_HEALTHCHECK = "HealthCheck";// 健康查体
    private final static FDbManager fdbManage = NovaApplication.fdbmanger;
    public static final String TYPE_OLD_PHYSIQUE = "old";// 老年人中医药健康管理
    public static final String TYPE_OLD_SELF_HELP = "old1";// 老年人自理能力
    public static final String TYPE_QYYH = "qyyh";// 签约用户
    public static final String TYPE_FWMYD = "fwmyd";// 服务满意度用户
    public static final String TYPE_PICTURE = "picture";// 图片
    public static final String TYPE_XSRFS = "xsrfs";//新生儿访视
    public static final String TYPE_ONEMONTH = "oneMonth";//满月龄访视
    public static final String TYPE_THREEMONTH = "threeMonth";//三月龄访视
    public static final String TYPE_SIXMONTH = "sixMonth";//六月龄访视
    public static final String TYPE_EIGHTMONTH = "eightMonth";//八月龄访视
    public static final String TYPE_12MONTH = "12Month";//12月龄访视
    public static final String TYPE_18MONTH = "18Month";//18月龄访视
    public static final String TYPE_24MONTH = "24Month";//24月龄访视
    public static final String TYPE_30MONTH = "30Month";//30月龄访视
    public static final String TYPE_3YEAR = "3Year";//3岁龄访视
    public static final String TYPE_4YEAR = "4Year";//4岁龄访视
    public static final String TYPE_5YEAR = "5Year";//5岁龄访视
    public static final String TYPE_6YEAR = "6Year";//6岁龄访视

    @SuppressWarnings("finally")
    public static int[] jx_data(String result, String upType) {
        JSONArray jsons;
        int i1 = 0;
        int i2 = 0;
        try {
            jsons = new JSONArray(result);
            int length = jsons.length();
            if (length > 0) {
                for (int i = 0; i < length; i++) {

                    JSONObject obj = jsons.getJSONObject(i);
                    String state = obj.getString("state");
                    String personalid = obj.getString("personalid");
                    String cardid = "";
                    if (obj.has("cardid")) {
                        cardid = obj.getString("cardid");
                    }
                    String cardNum = "";
                    if (obj.has("cardNum")) {
                        cardNum = obj.getString("cardNum");
                    }
                    if (state.equals("000")) {
                        i1++;
                        // 插入成功
                        if (upType.equals(TYPE_PERSONALINFO)) {
                            // 个人基本信息
                            List<ARCHIVE_BASEINFO> findAllByWhere = fdbManage.findAllByWhere(ARCHIVE_BASEINFO.class,
                                    "IDCARD ='" + cardid + "'");
                            if (findAllByWhere.size() > 0) {
                                ARCHIVE_BASEINFO archive_BASEINFO = findAllByWhere.get(0);
                                archive_BASEINFO.setIS_UPDATE("N");
                                archive_BASEINFO.setARCHIVEID(personalid);
                                fdbManage.updata(archive_BASEINFO, "IDCARD ='" + cardid + "'");
                            }
                        } else if (upType.equals(TYPE_HYPERTENSION)) {
                            // 高血压
                            List<CD_HYPERTENSIONFOLLOWUP> findAllByWhere = fdbManage
                                    .findAllByWhere(CD_HYPERTENSIONFOLLOWUP.class, "IDCARD ='" + cardid + "'");
                            if (findAllByWhere.size() > 0) {
                                CD_HYPERTENSIONFOLLOWUP cd_HYPERTENSIONFOLLOWUP = findAllByWhere.get(0);
                                cd_HYPERTENSIONFOLLOWUP.setIS_UPDATE("N");
                                fdbManage.updata(cd_HYPERTENSIONFOLLOWUP, "IDCARD ='" + cardid + "'");
                            }
                        } else if (upType.equals(TYPE_DIABETES)) {
                            // 糖尿病
                            List<CD_DIABETESFOLLOWUP> findAllByWhere = fdbManage
                                    .findAllByWhere(CD_DIABETESFOLLOWUP.class, "IDCARD ='" + cardid + "'");
                            if (findAllByWhere.size() > 0) {
                                CD_DIABETESFOLLOWUP cd_DIABETESFOLLOWUP = findAllByWhere.get(0);
                                cd_DIABETESFOLLOWUP.setIS_UPDATE("N");
                                fdbManage.updata(cd_DIABETESFOLLOWUP, "IDCARD ='" + cardid + "'");
                            }
                        } else if (upType.equals(TYPE_HEALTHCHECK)) {
                            // 健康体检_客户体检基本信息(症状)
                            List<ARCHIVE_CUSTOMERBASEINFO> customerbaseinfos = fdbManage
                                    .findAllByWhere(ARCHIVE_CUSTOMERBASEINFO.class, "IDCARD ='" + cardid + "'");
                            if (customerbaseinfos.size() > 0) {
                                ARCHIVE_CUSTOMERBASEINFO cd_CUSTOMERBASEINFO = customerbaseinfos.get(0);
                                cd_CUSTOMERBASEINFO.setIS_UPDATE("N");
                                fdbManage.updata(cd_CUSTOMERBASEINFO, "IDCARD ='" + cardid + "'");
                            }

                        } else if (upType.equals(TYPE_QYYH)) {
                            //签约用户
                            List<JTYSCustomerDoctorCont> jtysCustomerDoctorConts = fdbManage
                                    .findAllByWhere(JTYSCustomerDoctorCont.class, "cardNum ='" + cardNum + "'");
                            if (jtysCustomerDoctorConts.size() > 0) {
                                JTYSCustomerDoctorCont jtysCustomerDoctorCont = jtysCustomerDoctorConts.get(0);
                                jtysCustomerDoctorCont.setSign("1");
                                fdbManage.updata(jtysCustomerDoctorCont, "cardNum ='" + cardNum + "'");
                            }

                        } else if (upType.equals(TYPE_FWMYD)) {
                            //服务满意度用户
                            List<JTYSDoctorEvaluateBean> jtysDoctorEvaluateBeen = fdbManage
                                    .findAllByWhere(JTYSDoctorEvaluateBean.class, "IDCARD ='" + cardNum + "'");
                            if (jtysDoctorEvaluateBeen.size() > 0) {
                                JTYSDoctorEvaluateBean jtysDoctorEvaluateBean = jtysDoctorEvaluateBeen.get(0);
                                jtysDoctorEvaluateBean.setSign("1");
                                fdbManage.updata(jtysDoctorEvaluateBean, "cardNum ='" + cardNum + "'");
                            }

                        } else if (upType.equals(TYPE_PICTURE)) {
                        }else if (upType.equals(TYPE_OLD_PHYSIQUE)){
                            List<OLD_MEDICINE_CN> old_medicine_cns = fdbManage
                                    .findAllByWhere(OLD_MEDICINE_CN.class, "IDCARD ='" + cardid + "'");
                            if (old_medicine_cns.size() > 0) {
                                OLD_MEDICINE_CN old_medicine_cn = old_medicine_cns.get(0);
                                old_medicine_cn.setIS_UPDATE("N");
                                fdbManage.updata(old_medicine_cn, "IDCARD ='" + cardid + "'");
                            }

                        }else if (upType.equals(TYPE_OLD_SELF_HELP)){
                            List<OLDER_SELFCAREABILITY> older_selfcareabilities = fdbManage
                                    .findAllByWhere(OLDER_SELFCAREABILITY.class, "IDCARD ='" + cardid + "'");
                            if (older_selfcareabilities.size() > 0) {
                                OLDER_SELFCAREABILITY older_selfcareability = older_selfcareabilities.get(0);
                                older_selfcareability.setIS_UPDATE("N");
                                fdbManage.updata(older_selfcareability, "IDCARD ='" + cardid + "'");
                            }
                        }
                        else if (upType.equals(TYPE_XSRFS)) {
                            //新生儿访视
                            List<CHILD_NEWBORN_FOLLOWUP> child_newborn_followups = fdbManage
                                    .findAllByWhere(CHILD_NEWBORN_FOLLOWUP.class, "ARCHIVEID ='" + personalid + "'");
                            if (child_newborn_followups.size() > 0) {
                                CHILD_NEWBORN_FOLLOWUP child_newborn_followup = child_newborn_followups.get(0);
                                child_newborn_followup.setIS_UPDATE("N");
                                fdbManage.updata(child_newborn_followup, "ARCHIVEID ='" + personalid + "'");
                            }
                        }else if (upType.equals(TYPE_ONEMONTH)){
                            //满月龄
                            List<CHILD_WITHIN_ONE_YEAR_OLD> childWithinOneYearOlds = fdbManage
                                    .findAllByWhere(CHILD_WITHIN_ONE_YEAR_OLD.class, "ARCHIVEID ='" + personalid + "' AND FLAG = '1'");
                            if (childWithinOneYearOlds.size() > 0) {
                                CHILD_WITHIN_ONE_YEAR_OLD childWithinOneYearOld = childWithinOneYearOlds.get(0);
                                childWithinOneYearOld.setIS_UPDATE("N");
                                fdbManage.updata(childWithinOneYearOld, "ARCHIVEID ='" + personalid + "'AND FLAG = '1'");
                            }
                        }else if (upType.equals(TYPE_THREEMONTH)){
                            //三月龄
                            List<CHILD_WITHIN_ONE_YEAR_OLD> childWithinOneYearOlds = fdbManage
                                    .findAllByWhere(CHILD_WITHIN_ONE_YEAR_OLD.class, "ARCHIVEID ='" + personalid + "' AND FLAG = '2'");
                            if (childWithinOneYearOlds.size() > 0) {
                                CHILD_WITHIN_ONE_YEAR_OLD childWithinOneYearOld = childWithinOneYearOlds.get(0);
                                childWithinOneYearOld.setIS_UPDATE("N");
                                fdbManage.updata(childWithinOneYearOld, "ARCHIVEID ='" + personalid + "'AND FLAG = '2'");
                            }
                        }else if (upType.equals(TYPE_SIXMONTH)){
                            //六月龄
                            List<CHILD_WITHIN_ONE_YEAR_OLD> childWithinOneYearOlds = fdbManage
                                    .findAllByWhere(CHILD_WITHIN_ONE_YEAR_OLD.class, "ARCHIVEID ='" + personalid + "' AND FLAG = '3'");
                            if (childWithinOneYearOlds.size() > 0) {
                                CHILD_WITHIN_ONE_YEAR_OLD childWithinOneYearOld = childWithinOneYearOlds.get(0);
                                childWithinOneYearOld.setIS_UPDATE("N");
                                fdbManage.updata(childWithinOneYearOld, "ARCHIVEID ='" + personalid + "'AND FLAG = '3'");
                            }
                        }else if (upType.equals(TYPE_EIGHTMONTH)){
                            //八月龄
                            List<CHILD_WITHIN_ONE_YEAR_OLD> childWithinOneYearOlds = fdbManage
                                    .findAllByWhere(CHILD_WITHIN_ONE_YEAR_OLD.class, "ARCHIVEID ='" + personalid + "' AND FLAG = '4'");
                            if (childWithinOneYearOlds.size() > 0) {
                                CHILD_WITHIN_ONE_YEAR_OLD childWithinOneYearOld = childWithinOneYearOlds.get(0);
                                childWithinOneYearOld.setIS_UPDATE("N");
                                fdbManage.updata(childWithinOneYearOld, "ARCHIVEID ='" + personalid + "'AND FLAG = '4'");
                            }
                        }else if (upType.equals(TYPE_12MONTH)){
                            //12月龄
                            List<CHILD_ONE2THREE_YEAR_OLD> childOne2THREEYearOlds = fdbManage
                                    .findAllByWhere(CHILD_ONE2THREE_YEAR_OLD.class, "ARCHIVEID ='" + personalid + "' AND FLAG = '1'");
                            if (childOne2THREEYearOlds.size() > 0) {
                                CHILD_ONE2THREE_YEAR_OLD childOne2THREEYearOld = childOne2THREEYearOlds.get(0);
                                childOne2THREEYearOld.setIS_UPDATE("N");
                                fdbManage.updata(childOne2THREEYearOld, "ARCHIVEID ='" + personalid + "'AND FLAG = '1'");
                            }
                        }else if (upType.equals(TYPE_18MONTH)){
                            //18月龄
                            List<CHILD_ONE2THREE_YEAR_OLD> childOne2THREEYearOlds = fdbManage
                                    .findAllByWhere(CHILD_ONE2THREE_YEAR_OLD.class, "ARCHIVEID ='" + personalid + "' AND FLAG = '2'");
                            if (childOne2THREEYearOlds.size() > 0) {
                                CHILD_ONE2THREE_YEAR_OLD childOne2THREEYearOld = childOne2THREEYearOlds.get(0);
                                childOne2THREEYearOld.setIS_UPDATE("N");
                                fdbManage.updata(childOne2THREEYearOld, "ARCHIVEID ='" + personalid + "'AND FLAG = '2'");
                            }
                        }else if (upType.equals(TYPE_24MONTH)){
                            //24月龄
                            List<CHILD_ONE2THREE_YEAR_OLD> childOne2THREEYearOlds = fdbManage
                                    .findAllByWhere(CHILD_ONE2THREE_YEAR_OLD.class, "ARCHIVEID ='" + personalid + "' AND FLAG = '3'");
                            if (childOne2THREEYearOlds.size() > 0) {
                                CHILD_ONE2THREE_YEAR_OLD childOne2THREEYearOld = childOne2THREEYearOlds.get(0);
                                childOne2THREEYearOld.setIS_UPDATE("N");
                                fdbManage.updata(childOne2THREEYearOld, "ARCHIVEID ='" + personalid + "'AND FLAG = '3'");
                            }
                        }else if (upType.equals(TYPE_30MONTH)){
                            //30月龄
                            List<CHILD_ONE2THREE_YEAR_OLD> childOne2THREEYearOlds = fdbManage
                                    .findAllByWhere(CHILD_ONE2THREE_YEAR_OLD.class, "ARCHIVEID ='" + personalid + "' AND FLAG = '4'");
                            if (childOne2THREEYearOlds.size() > 0) {
                                CHILD_ONE2THREE_YEAR_OLD childOne2THREEYearOld = childOne2THREEYearOlds.get(0);
                                childOne2THREEYearOld.setIS_UPDATE("N");
                                fdbManage.updata(childOne2THREEYearOld, "ARCHIVEID ='" + personalid + "'AND FLAG = '4'");
                            }
                        }else if (upType.equals(TYPE_3YEAR)){
                            //3岁龄
                            List<CHILD_THREE2SIX_YEAR_OLD> childThree2SixYearOlds = fdbManage
                                    .findAllByWhere(CHILD_THREE2SIX_YEAR_OLD.class, "ARCHIVEID ='" + personalid + "' AND FLAG = '3'");
                            if (childThree2SixYearOlds.size() > 0) {
                                CHILD_THREE2SIX_YEAR_OLD childThree2SixYearOld = childThree2SixYearOlds.get(0);
                                childThree2SixYearOld.setIS_UPDATE("N");
                                fdbManage.updata(childThree2SixYearOld, "ARCHIVEID ='" + personalid + "'AND FLAG = '3'");
                            }
                        }else if (upType.equals(TYPE_4YEAR)){
                            //4岁龄
                            List<CHILD_THREE2SIX_YEAR_OLD> childThree2SixYearOlds = fdbManage
                                    .findAllByWhere(CHILD_THREE2SIX_YEAR_OLD.class, "ARCHIVEID ='" + personalid + "' AND FLAG = '4'");
                            if (childThree2SixYearOlds.size() > 0) {
                                CHILD_THREE2SIX_YEAR_OLD childThree2SixYearOld = childThree2SixYearOlds.get(0);
                                childThree2SixYearOld.setIS_UPDATE("N");
                                fdbManage.updata(childThree2SixYearOld, "ARCHIVEID ='" + personalid + "'AND FLAG = '4'");
                            }
                        }else if (upType.equals(TYPE_5YEAR)){
                            //5岁龄
                            List<CHILD_THREE2SIX_YEAR_OLD> childThree2SixYearOlds = fdbManage
                                    .findAllByWhere(CHILD_THREE2SIX_YEAR_OLD.class, "ARCHIVEID ='" + personalid + "' AND FLAG = '5'");
                            if (childThree2SixYearOlds.size() > 0) {
                                CHILD_THREE2SIX_YEAR_OLD childThree2SixYearOld = childThree2SixYearOlds.get(0);
                                childThree2SixYearOld.setIS_UPDATE("N");
                                fdbManage.updata(childThree2SixYearOld, "ARCHIVEID ='" + personalid + "'AND FLAG = '5'");
                            }
                        }else if (upType.equals(TYPE_6YEAR)){
                            //6岁龄
                            List<CHILD_THREE2SIX_YEAR_OLD> childThree2SixYearOlds = fdbManage
                                    .findAllByWhere(CHILD_THREE2SIX_YEAR_OLD.class, "ARCHIVEID ='" + personalid + "' AND FLAG = '6'");
                            if (childThree2SixYearOlds.size() > 0) {
                                CHILD_THREE2SIX_YEAR_OLD childThree2SixYearOld = childThree2SixYearOlds.get(0);
                                childThree2SixYearOld.setIS_UPDATE("N");
                                fdbManage.updata(childThree2SixYearOld, "ARCHIVEID ='" + personalid + "'AND FLAG = '6'");
                            }
                        }
                    } else {
                        i2++;
                    }
                }
            }
        } catch (JSONException e) {
            Log.e("JSONException", e.getMessage() + "---" + e.getCause());
        } finally {
            return new int[]{i1, i2};
        }

    }
}
